﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Ornithopter.FlashDiskHelper
{
    public static class Animations
    {
        public static Storyboard Popup(UIElement element)
        {
            Action<DoubleAnimationUsingKeyFrames> addFrames = new Action<DoubleAnimationUsingKeyFrames>(da=>
                {
                    EasingDoubleKeyFrame kf1 = new EasingDoubleKeyFrame() { 
                        KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0.0))
                        , Value = 0 };
                    EasingDoubleKeyFrame kf2 = new EasingDoubleKeyFrame()
                    {
                        KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(600.0))
                        ,
                        Value = 1
                        ,
                        EasingFunction = new ElasticEase()
                            {
                                EasingMode = EasingMode.EaseOut,
                                Springiness = 4,
                                Oscillations = 1
                            }
                    };
                    da.KeyFrames.Add(kf1);
                    da.KeyFrames.Add(kf2);
                });

            

            Storyboard sb = new Storyboard();
            DoubleAnimationUsingKeyFrames dax = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames day = new DoubleAnimationUsingKeyFrames();
            Storyboard.SetTarget(dax, element);
            Storyboard.SetTarget(day, element);
            Storyboard.SetTargetProperty(dax, new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
            Storyboard.SetTargetProperty(day, new PropertyPath("RenderTransform.(ScaleTransform.ScaleY)"));
            addFrames(dax); 
            addFrames(day);
            sb.Children.Add(dax);
            sb.Children.Add(day);

            Point originCache = element.RenderTransformOrigin;
            Transform transformCache = element.RenderTransform;
            element.RenderTransform = new ScaleTransform(0.0, 0.0);
            element.RenderTransformOrigin = new Point(0.5, 0.5);
            EventHandler completed = null;
            completed = (s, e) =>
                {
                    sb.Completed -= completed;
                    element.RenderTransform = transformCache;
                    transformCache = null;
                    element.RenderTransformOrigin = originCache;
                };
            sb.Completed += completed;
            return sb;
        }

        public static Storyboard Popout(UIElement element)
        {
            Action<DoubleAnimationUsingKeyFrames> addFrames = new Action<DoubleAnimationUsingKeyFrames>(da =>
            {
                EasingDoubleKeyFrame kf1 = new EasingDoubleKeyFrame()
                {
                    KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0.0))
                    ,
                    Value = 1
                };
                EasingDoubleKeyFrame kf2 = new EasingDoubleKeyFrame()
                {
                    KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(600.0))
                    ,
                    Value = 0
                    ,
                    EasingFunction = new ElasticEase()
                    {
                        EasingMode = EasingMode.EaseIn,
                        Springiness = 4,
                        Oscillations = 1
                    }
                };
                da.KeyFrames.Add(kf1);
                da.KeyFrames.Add(kf2);
            });



            Storyboard sb = new Storyboard();
            DoubleAnimationUsingKeyFrames dax = new DoubleAnimationUsingKeyFrames();
            DoubleAnimationUsingKeyFrames day = new DoubleAnimationUsingKeyFrames();
            Storyboard.SetTarget(dax, element);
            Storyboard.SetTarget(day, element);
            Storyboard.SetTargetProperty(dax, new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
            Storyboard.SetTargetProperty(day, new PropertyPath("RenderTransform.(ScaleTransform.ScaleY)"));
            addFrames(dax);
            addFrames(day);
            sb.Children.Add(dax);
            sb.Children.Add(day);

            Point originCache = element.RenderTransformOrigin;
            Transform transformCache = element.RenderTransform;
            element.RenderTransform = new ScaleTransform(0.0, 0.0);
            element.RenderTransformOrigin = new Point(0.5, 0.5);
            EventHandler completed = null;
            completed = (s, e) =>
            {
                sb.Completed -= completed;
                element.RenderTransform = transformCache;
                transformCache = null;
                element.RenderTransformOrigin = originCache;
            };
            sb.Completed += completed;
            return sb;
        }
    }
}
